package net.keebook.webapp.bean;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import net.keebook.webapp.dao.AccountConfirmationDao;
import net.keebook.webapp.dao.SettingDao;
import net.keebook.webapp.dao.UserDao;
import net.keebook.webapp.entity.User;
import net.keebook.webapp.entity.User.LoginMethod;
import net.keebook.webapp.model.SessionModel;
import net.keebook.webapp.util.AccountConfirmationMail;
import net.keebook.webapp.util.BCrypt;
import net.keebook.webapp.util.Messages;
import net.keebook.webapp.util.SettingId;

@ManagedBean
@RequestScoped
public class MigrateAccountBean {
	private String provider;
	private String email;
	private String password;
	private Boolean passwordChecked;
	private SessionModel sessionModel;
	@Inject
	private SettingDao settingDao;
	@Inject
	private UserDao userDao;
	@Inject
	private AccountConfirmationDao accountConfirmationDao;
	@EJB
	private OAuthBean oAuthBean;

	@PostConstruct
	public void init() throws IOException {
		setSessionModel(SessionModel.getInstance(FacesContext.getCurrentInstance()));
	}
	
	public void migratePassword() throws IOException {
		User user = getUserDao().get(getSessionModel().getUserId());
		
		user.setEmail(getEmail());
		user.setUsername(getEmail());
		user.setHashedPassword(BCrypt.hashpw(getPassword(), BCrypt.gensalt()));
		user.setLoginMethod(LoginMethod.PASSWORD);
		user.setConfirmed(false);
		getUserDao().update(user);
		
		AccountConfirmationMail.send(user, getAccountConfirmationDao(), getSettingDao());
		
		FacesContext.getCurrentInstance().getExternalContext().redirect("migrateaccount.xhtml?msg="+Messages.ACCOUNT_MIGRATED);
	}
	
	public void migrate() throws IOException {
		SessionModel sessionModel = getSessionModel();
		User user = getUserDao().get(sessionModel.getUserId());
		
		String provider = getProvider();
		if ("password".equals(provider) && getPasswordLoginEnabled()) {
			if (user.getEmail() != null && !"".equals(user.getEmail()) && user.getHashedPassword() != null && !"".equals(user.getHashedPassword())) {
				user.setLoginMethod(LoginMethod.PASSWORD);
				getUserDao().update(user);
				FacesContext.getCurrentInstance().getExternalContext().redirect("migrateaccount.xhtml?msg="+Messages.ACCOUNT_MIGRATED);
			} else {
				FacesContext.getCurrentInstance().getExternalContext().redirect("migrateaccount2.xhtml");
			}
		} else if ("google".equals(provider) && getGoogleLoginEnabled()) {
			sessionModel.setMigrateAccount(true);
			getoAuthBean().loginGoogle(FacesContext.getCurrentInstance());
		} else if ("twitter".equals(provider) && getTwitterLoginEnabled()) {
			sessionModel.setMigrateAccount(true);
			getoAuthBean().loginTwitter(FacesContext.getCurrentInstance(), getSessionModel());
		} else if ("facebook".equals(provider) && getFacebookLoginEnabled()) {
			sessionModel.setMigrateAccount(true);
			getoAuthBean().loginFacebook(FacesContext.getCurrentInstance());
		}
	}
	
	public Map<String, String> getProviders() {
		User user = getUserDao().get(getSessionModel().getUserId());
		
		Map<String, String> list = new LinkedHashMap<String, String>();
		if (!user.getLoginMethod().equals(LoginMethod.PASSWORD)) {
			list.put("Email and password", "password");
		}
		if (getFacebookLoginEnabled()) list.put("Facebook", "facebook");
		if (getGoogleLoginEnabled()) list.put("Google", "google");
		if (getTwitterLoginEnabled()) list.put("Twitter", "twitter");
		return list;
	}
	
	public Boolean getGoogleLoginEnabled() {
		return getSettingDao().get(SettingId.GOOGLE_LOGIN).getBoolean();
	}
	public Boolean getPasswordLoginEnabled() {
		return getSettingDao().get(SettingId.PASSWORD_LOGIN).getBoolean();
	}
	public Boolean getFacebookLoginEnabled() {
		return getSettingDao().get(SettingId.FACEBOOK_LOGIN).getBoolean();
	}
	public Boolean getTwitterLoginEnabled() {
		return getSettingDao().get(SettingId.TWITTER_LOGIN).getBoolean();
	}
	
	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		this.provider = provider;
	}

	public SessionModel getSessionModel() {
		return sessionModel;
	}

	public void setSessionModel(SessionModel sessionModel) {
		this.sessionModel = sessionModel;
	}

	public SettingDao getSettingDao() {
		return settingDao;
	}

	public void setSettingDao(SettingDao settingDao) {
		this.settingDao = settingDao;
	}

	public OAuthBean getoAuthBean() {
		return oAuthBean;
	}

	public void setoAuthBean(OAuthBean oAuthBean) {
		this.oAuthBean = oAuthBean;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public Boolean getPasswordChecked() {
		return passwordChecked;
	}

	public void setPasswordChecked(Boolean passwordChecked) {
		this.passwordChecked = passwordChecked;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public AccountConfirmationDao getAccountConfirmationDao() {
		return accountConfirmationDao;
	}

	public void setAccountConfirmationDao(AccountConfirmationDao accountConfirmationDao) {
		this.accountConfirmationDao = accountConfirmationDao;
	}
}
